package Components.progress
{
	import Components.config.ImageConfig;
	
	import core.StageManager;
	
	import events.GlobalEvent;
	import events.SDispatcher;
	import events.StageEvent;
	
	import flash.display.Shape;
	import flash.display.Sprite;

	public class ProgressController
	{
		private var _back:Sprite;
		
		private var _container:Sprite;
		
		private var _bar:Shape;
		
		private var _current:int = 0;
		private var _max:int = 1;
		
		public function ProgressController()
		{
			init();
		}
		
		private function init():void
		{
			_back = new Sprite();
			_back.graphics.beginFill( 0, 0.3 );
			_back.graphics.drawRect( 0, 0, 1, 1 );
			_back.graphics.endFill();
			
			_container = new Sprite();
			_container.graphics.beginFill( 0x333333 );
			_container.graphics.drawRect( 0, 0, 250, 100 );
			_container.graphics.endFill();
			_container.graphics.lineStyle( 1 );
			_container.graphics.drawRect( 0, 45, 250, 10 );
			
			_bar = new Shape();
			_bar.graphics.beginFill( 0xff0000 );
			_bar.graphics.drawRect( 0, 0, 250, 10 );
			_bar.graphics.endFill();
			_bar.y = 45;
			_container.addChild( _bar );
			
			SDispatcher.addEventListenr( GlobalEvent.OUT_COMPLETE, onComplete );
			SDispatcher.addEventListenr( GlobalEvent.ONE_BITMAP_COMPLETE, onOneBitmapComplete );
		}
		
		public function show():void
		{
			_current = 0;
			_max = ImageConfig.arrOutBMClass.length;
			
			StageManager.stage.addChild( _back );
			StageManager.stage.addChild( _container );
			
			onResize(null);
			StageManager.instance.addEventListener( StageEvent.RESIZE, onResize );
		}
		
		private function onOneBitmapComplete( e:GlobalEvent ):void
		{
			_current++;
			if( _current > _max ) _current = _max;
			_bar.scaleX = _current / _max;
		}
		
		private function onComplete( e:GlobalEvent ):void
		{
			if( _back.parent )
			{
				StageManager.stage.removeChild( _back );
				StageManager.stage.removeChild( _container );
				
				StageManager.instance.removeEventListener( StageEvent.RESIZE, onResize );
			}
		}
		
		private function onResize( e:StageEvent ):void
		{
			var w:int = StageManager.stageWidth;
			var h:int = StageManager.stageHeight;
			_back.width = w;
			_back.height = h;
			
			_container.x = (w - _container.width)/2;
			_container.y = (h - _container.height)/2;
		}
	}
}












